<?php

/**
 *@作者:MissZhang
 *@邮箱:<787727147@qq.com>
 *@创建时间:2022/10/12 18:14
 *@说明:认养商品模型
 */
namespace app\common\model;


use think\facade\Db;
use think\Model;

class FosterOrder extends OrderCommon
{
    protected $pk = "order_id";
    // 定义时间戳字段名
    protected $createTime = 'add_time';
    //修改format类型
    protected $dateFormat = "Y-m-d H:i:s";
    protected $type=[
        'goods_price'   =>  'float',
        'total_amount'  =>  'float',
        'ship_price'    =>  'float',
        'order_amount'  =>  'float',
        'day_sy'  =>  'float',
        'speed'  =>  'float',
        'yield'  =>  'float',
        'yet_sy'  =>  'float',
        'surplus_yield'  =>  'float',
        'extra_text'    =>  'serialize',
    ];
    public function getPkAttr($value,$data) {
        return $this->pk;
    }
    public function user(){
        return $this->hasOne(Users::class, 'user_id','user_id');
    }
    public function qianbao(){
        return $this->hasOne(Wallet::class, 'id','wid');
    }
    public function goods(){
        return $this->hasOne(FosterGoods::class, 'goods_id','goods_id');
    }
    public function orderExtra(){
        return $this->hasMany(FosterOrderExtra::class, 'oid');
    }
    public function store()
    {
        return $this->hasOne(Store::class, 'store_id', 'store_id');
    }
    /********************{此处修改}********************/
    public function getSurplusYieldAttr($value) {
        return $this->yield*10-$this->yet_sy;
    }
    //获取剩余认养周期
    public function getSurplusDayTextAttr($value,$data) {
        $surplus_yield=$this->surplus_yield;
        if ($surplus_yield<=0){
            return 0;
        }
        //计算方式 (总收益-已领取收益)/(每日收益+加速)
//        $yield=$data['yield'];
//        $yet_sy=$data['yet_sy'];
//        $surplus_yield=$data['surplus_yield']*10;
        $day_sy=($data['day_sy']*$data['goods_num'])+$data['speed'];
        return $day_sy>0?ceil(($surplus_yield)/$day_sy):0;
    }
    public function getOrderStatusTextAttr($value,$data) {
        $status = [-2=>'已取消',-1=>'待支付',1=>'认养中',2=>'已完成'];
        return $status[$data['order_status']];
    }
    public function getFeedStatusTextAttr($value,$data) {
        $status = [1=>'待喂养',2=>'待领取',3=>'已领取'];
        return $status[$data['feed_status']];
    }
    public function getPayTimeTextAttr($value,$data) {
        if ($data['pay_time']){
            return date($this->dateFormat,$data['pay_time']);
        }
        return '---';
    }
    public function getEndTimeTextAttr($value,$data) {
        if ($data['end_time']){
            return date($this->dateFormat,$data['end_time']);
        }
        return '---';
    }
    public function getCancelTimeTextAttr($value,$data) {
        if ($data['cancel_time']){
            return date($this->dateFormat,$data['cancel_time']);
        }
        return '---';
    }
    public function getTimeTextAttr($value,$data) {
        return date('Y-m-d',$data['add_time']);
    }
}
